package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.mystatic.publicstatic;
import longcaisuyun.longcai.com.net.PostEditPassword;


public class LiShiActivity extends Activity implements View.OnClickListener {

    TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6;
    Button bt_next;
    LinearLayout l_mima;
    int num = 0;
    List<TextView> list = new ArrayList<TextView>();
    int j = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_li_shi);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        publicstatic.list_mima.add(this);
        ViewInit();
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        l_mima = (LinearLayout) findViewById(R.id.l_mima);
        l_mima.setOnClickListener(this);
        list.add(tv_1);
        list.add(tv_2);
        list.add(tv_3);
        list.add(tv_4);
        list.add(tv_5);
        list.add(tv_6);

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.l_mima:
                showpop();
                break;
            case R.id.b_1:
                add("1");
                break;
            case R.id.b_2:
                add("2");
                break;
            case R.id.b_3:
                add("3");
                break;
            case R.id.b_4:
                add("4");
                break;
            case R.id.b_5:
                add("5");
                break;
            case R.id.b_6:
                add("6");
                break;
            case R.id.b_7:
                add("7");
                break;
            case R.id.b_8:
                add("8");
                break;
            case R.id.b_9:
                add("9");
                break;
            case R.id.b_0:
                add("0");
                break;
            case R.id.b_del:
                del();
                break;
        }
    }

    public String getpassword() {
        String s = "";
        for (int j = 0; j < list.size(); j++) {
            s += list.get(j).getText().toString();
        }
        return s;
    }

    public void add(String j) {

        if (num < 6) {
            list.get(num).setText(j);
            if (num < 6)
                num++;
        }
        if (num == 6) {
            MyApplication.myviewutil.startProgressDialog(LiShiActivity.this);
            new PostEditPassword(MyApplication.myPreferences.readUid(), getpassword() ,getpassword(),"2", new AsyCallBack<PostEditPassword.Info>() {
                @Override
                public void onSuccess(String toast, int type, PostEditPassword.Info info) throws Exception {
                    super.onSuccess(toast, type, info);
                    if (info.message.equals("2")) {
                        Toast.makeText(LiShiActivity.this, "密码输入错误请重新输入", Toast.LENGTH_SHORT).show();
                    } else if (info.message.equals("1")){
                        Intent i = new Intent();
                        i.setClass(LiShiActivity.this, SzmmActivity.class);
                        i.putExtra("model", "2");
                        i.putExtra("pass",getpassword());
                        startActivity(i);
                        alldel();
                    }
                    MyApplication.myviewutil.stopProgressDialog();
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    MyApplication.myviewutil.stopProgressDialog();
                }

            }).execute(LiShiActivity.this);

        }
    }

    public void del() {
        Log.e("num", num + "");
        if (num == 6) {
            num = 5;
            list.get(num).setText("");
        } else {
            list.get(num).setText("");
        }

        if (num > 0) num--;
    }

    public void alldel() {
        for (int j = 0; j < list.size(); j++) {
            list.get(j).setText("");

        }
        num = 0;
    }

    public void showpop() {
        View view = View.inflate(LiShiActivity.this, R.layout.item_input, null);

        final PopupWindow window = new PopupWindow(MyApplication.ScaleScreenHelper.loadView((ViewGroup) view),
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 设置popWindow弹出窗体可点击
        window.setFocusable(true);
        window.setOutsideTouchable(false);
        window.setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable();
        window.setBackgroundDrawable(dw);
        window.showAtLocation(l_mima, Gravity.BOTTOM, 0, 0);
        Button b_1 = (Button) view.findViewById(R.id.b_1);
        Button b_2 = (Button) view.findViewById(R.id.b_2);
        Button b_3 = (Button) view.findViewById(R.id.b_3);
        Button b_4 = (Button) view.findViewById(R.id.b_4);
        Button b_5 = (Button) view.findViewById(R.id.b_5);
        Button b_6 = (Button) view.findViewById(R.id.b_6);
        Button b_7 = (Button) view.findViewById(R.id.b_7);
        Button b_8 = (Button) view.findViewById(R.id.b_8);
        Button b_9 = (Button) view.findViewById(R.id.b_9);
        Button b_0 = (Button) view.findViewById(R.id.b_0);
        Button b_del = (Button) view.findViewById(R.id.b_del);
        b_1.setOnClickListener(this);
        b_2.setOnClickListener(this);
        b_3.setOnClickListener(this);
        b_4.setOnClickListener(this);
        b_5.setOnClickListener(this);
        b_6.setOnClickListener(this);
        b_7.setOnClickListener(this);
        b_8.setOnClickListener(this);
        b_9.setOnClickListener(this);
        b_0.setOnClickListener(this);
        b_del.setOnClickListener(this);

    }

}
